package www.lagou.dao;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import www.lagou.utils.DruidUtils;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

public class PhoneDao {

    QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());

    //自定义成员方法查询价格高于2000元，生产日期是2019年之前的所有手机
    public void selectPriceByProdate(){
        String sql = "select * from phone where price > ? and prodate < ?";
        try {
            List<Object[]> list = qr.query(sql, new ArrayListHandler(), 2000, "2019/01/01");
            for (Object[] objects : list) {
                System.out.println(Arrays.toString(objects));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //自定义成员方法查询所有颜色是白色的手机信息
    public void selectByColor(){
        String sql = "select * from phone where color = ?";
        try {
            List<Object[]> list = qr.query(sql, new ArrayListHandler(), "白色");
            for (Object[] objects : list) {
                System.out.println(Arrays.toString(objects));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
